﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParsecParser;
using System.Xml.Linq;
using System.IO;
using System.Deployment.Application;

namespace ParsecClient.Classes
{
    [Serializable]
    public class CharacterSettingsCollection
    {
        public List<CharacterSettings> CharacterSettings { get; set; }
        private static string CharacterSettingsFileName = "CharacterSettings.xml";
        private static System.Xml.Serialization.XmlSerializer CharacterSettingsFileSerializer = new System.Xml.Serialization.XmlSerializer(typeof(CharacterSettingsCollection));
        public static List<CharacterSettings> SaveAllCharacterSettings(List<CharacterSettings> settings)
        {
            string fullPath = Serializer.GetFullPath(CharacterSettingsFileName);
            CharacterSettingsCollection csc = new CharacterSettingsCollection() { CharacterSettings = settings };
            DeleteAllCharacterSettings();
            Serializer.SerializeDataToDisk(CharacterSettingsFileSerializer, csc, fullPath);
            return settings;
        }
        public static List<CharacterSettings> SaveCharacterSettings(CharacterSettings settings)
        {
            string fullPath = Serializer.GetFullPath(CharacterSettingsFileName);
            CharacterSettingsCollection csc = new CharacterSettingsCollection() { CharacterSettings = new List<CharacterSettings>() { settings } };
            //If file doesnt exist
            if (!File.Exists(fullPath))
            {
                Serializer.SerializeDataToDisk(CharacterSettingsFileSerializer, csc, fullPath);
            }
            else
            {
                //load file
                CharacterSettingsCollection fromDisk = (CharacterSettingsCollection)Serializer.DeserializeDataFromDisk(CharacterSettingsFileSerializer, fullPath, false);
                if (fromDisk != null)
                    csc = fromDisk;
                CharacterSettings existingSettings = csc.CharacterSettings.FirstOrDefault(x => x.CharacterName == settings.CharacterName);
                if (existingSettings == null)
                {
                    csc.CharacterSettings.Add(settings);
                }
                else
                {                    
                    csc.CharacterSettings[csc.CharacterSettings.IndexOf(existingSettings)] = settings;
                }

                //rewrite file
                Serializer.SerializeDataToDisk(CharacterSettingsFileSerializer, csc, fullPath);
            }
            return csc.CharacterSettings;
        }
        public static List<CharacterSettings> RetrieveCharacterSettings()
        {
            string fullPath = Serializer.GetFullPath(CharacterSettingsFileName);
            if (File.Exists(fullPath))
            {
                CharacterSettingsCollection csc = (CharacterSettingsCollection)Serializer.DeserializeDataFromDisk(CharacterSettingsFileSerializer, fullPath, false);
                return csc == null ? new List<CharacterSettings>() : csc.CharacterSettings;
            }
            else
                return new List<CharacterSettings>();
        }
        public static void DeleteAllCharacterSettings()
        {
            string fullPath = Serializer.GetFullPath(CharacterSettingsFileName);
            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
            }
        }
    }
}
